package com.yaozheng.vocationaltraining.view;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.activity.CourseCatalogActivity_;
import com.yaozheng.vocationaltraining.activity.HomeActivity;
import com.yaozheng.vocationaltraining.adapter.LearnListContentAdapter;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

@EViewGroup(R.layout.item_learn_list_content)
public class LearnListContentItemView extends LinearLayout {
    @ViewById
    TextView itemLearnListContentName;

    JSONObject itemData;
    @ViewById
    View learnListContentItemBorder;
    @ViewById
    ImageView itemLearnListContentImage;


    public LearnListContentItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LearnListContentItemView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public Intent getIntent() {
        return ((BaseActivity) getContext()).getIntent();
    }

    /**
     * 加载数据
     *
     * @param position
     */
    public void loadData(int position, JSONObject itemData, BitmapLoader bitmapLoader) {
        this.itemData = itemData;
        int id = TypeUtils.getJsonInteger(itemData, "id");
        if (id <= -1) {
            itemLearnListContentImage.setVisibility(INVISIBLE);
        } else {
            itemLearnListContentImage.setVisibility(VISIBLE);
            bitmapLoader.displayImage(TypeUtils.getJsonString(itemData, "cate_pic"), itemLearnListContentImage);
        }
//        if (itemData == null) {
//            itemLearnListContentImage.setImageResource(R.drawable.goods_subclass_img1_1);
//            itemLearnListContentImage.setVisibility(View.INVISIBLE);
//        } else {
//            String name = TypeUtils.getJsonString(itemData, "name");
//            itemLearnListContentImage.setVisibility(View.VISIBLE);
//            if ("宝沃由来".equals(name)) {
//                itemLearnListContentImage.setImageResource(R.drawable.brand_subclass_img1_1);
//            } else if ("宝沃归来".equals(name)) {
//                itemLearnListContentImage.setImageResource(R.drawable.brand_subclass_img2_2);
//            } else if ("宝沃未来".equals(name)) {
//                itemLearnListContentImage.setImageResource(R.drawable.brand_subclass_img3_3);
//            } else if ("BX7".equals(name)) {
//                itemLearnListContentImage.setImageResource(R.drawable.goods_subclass_img1_1);
//            } else if ("销售基础".equals(name)) {
//                itemLearnListContentImage.setImageResource(R.drawable.sales_subclass_img1_1);
//            } else if ("服务基础".equals(name)) {
//                itemLearnListContentImage.setImageResource(R.drawable.service_subclass_img1_1);
//            } else if ("技术基础".equals(name)) {
//                itemLearnListContentImage.setImageResource(R.drawable.technology_subclass_img1_1);
//            } else if ("岗位认证".equals(name)) {
//                itemLearnListContentImage.setImageResource(R.drawable.renzheng_subclass_img1_1);
//            } else {
//                itemLearnListContentImage.setImageResource(R.drawable.goods_subclass_img1_1);
//                itemLearnListContentImage.setVisibility(View.INVISIBLE);
//
//            }
//    }
//        itemLearnListContentName.setText(TypeUtils.getJsonString(itemData,
//                "name"));
//        itemLearnListContentName.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(TypeUtils.getJsonInteger(itemData, "imageResourcesId", R.drawable.brand_subclass_img1)), null, null, null);
//        if (position == size - 1) {
//        learnListContentItemBorder.setVisibility(View.GONE);
//        } else {
//            learnListContentItemBorder.setVisibility(View.VISIBLE);
//        }

    }

    /**
     * 进入课程目录
     */
    @Click(R.id.itemRootView)
    public void enterCourseCatalog() {
        if (itemLearnListContentImage.getVisibility() == View.VISIBLE) {
            if (getContext() instanceof HomeActivity) {
                HomeActivity homeActivity = (HomeActivity) getContext();
                homeActivity.setIntentString("learnTwoTitle", TypeUtils.getJsonString(itemData,
                        "name"));
                homeActivity.setIntentInt("subCatId", TypeUtils.getJsonInteger(itemData, "id"));
                homeActivity.setIntentInt("menuType", getIntent().getIntExtra("menuType", 0));
                homeActivity.openCourseCatalogFragment();
            } else {
                Intent intent = new Intent();
                intent.setClassName(getContext(),
                        Constants.COURSE_CATALOG_ACTIVITY_CLASS_NAME);
                intent.putExtra("title", TypeUtils.getJsonString(itemData,
                        "name"));
                intent.putExtra("subCatId", TypeUtils.getJsonInteger(itemData, "id"));
                intent.putExtra("menuType", getIntent().getIntExtra("menuType", 0));
                getContext().startActivity(intent);

            }
        }

    }
}
